<?php
class DepartmentAction extends Action {
	private $uid = '';
	/*======================================================================*\
		Function:	_initialize
		Purpose:	初始化一些参数，主要判断是否登录授权
		Input:		无	            空
		Output:		无      		空
	\*======================================================================*/
	public function _initialize(){
		
		$this->_uid = session('uid');
		if(!$this->_uid) $this->display("Public:login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
	
	/*======================================================================*\
		Function:	departmentAdd
		Purpose:	客户分类管理-添加分类
		Input:		department_name	 department_description
		Output:		模板输出      	#
	\*======================================================================*/
	public function departmentAdd(){
		$Department = M('Department');
		$param = $this->_param();
		$uid = $this->_uid;
		if($param['department_name']){
			$data = array(
				'owner_uid'	=> $uid,
				'department_name' => $param['department_name'],
				'department_description' => $param['department_description'],
				'status' => 1
			);
			$v = $Department->where($data)->find();
			if($v){
				$this->ajaxReturn(0,'部门已经存在',0);
			}else{
				$v = $Department->add($data);
				if($v){
					$this->ajaxReturn(1,'添加成功',1);
				}else{
					$this->ajaxReturn(0,'添加失败',0);
				}
			}
		}else{
			$this->ajaxReturn(0,'部门名不能为空',0);
		}
	}
	/*======================================================================*\
		Function:	departmentEdit
		Purpose:	客户分类管理- 修改分类
		Input:		category_id category_name
		Output:		模板输出      	#
	\*======================================================================*/
	public function departmentEdit(){
		$Category = M('Category');
		$get = $this->_get();
		$uid = $this->_uid;
		if($get['category_name'] && $get['category_id']){
			$data = array(
				'category_id'	=> $get['category_id'],
				'category_name' => $get['category_name']
			);
			$v = $Category->where("uid = '$uid'")->save($data);
			if($v){
				logs($uid,"分类名修改成功",1);
				$this->ajaxReturn(1,'分类名修改成功',1);
			}else{
				logs($uid,"分类名修改失败",0);
				$this->ajaxReturn(0,'分类名修改失败',0);
				
			}
		}else{
			$this->ajaxReturn(0,'分类名不能为空',0);
		}

	}
	/*======================================================================*\
		Function:	departmentDel
		Purpose:	客户分类管理- 删除分类
		Input:		category_id	 
		Output:		模板输出      	#
	\*======================================================================*/
	public function departmentDel(){
		$Category = M('Category');
		$get = $this->_get();
		$uid = $this->_uid;
		if($get['category_id']){
			$data = array(
				'category_id'	=> $get['category_id'],
				'owner_uid' => $uid
			);
			$v = $Category->where($data)->delete();
			if($v){
				logs($uid,"分类删除成功",1);
				$this->ajaxReturn(1,"分类删除成功",1);
			}else{
				logs($uid,"分类删除失败",0);
				$this->ajaxReturn(0,"分类删除失败",0);
				
			}
		}else{
			$this->ajaxReturn(0,'获取分类ID失败',0);
		}
	}
	/*======================================================================*\
		Function:	getDepartment
		Purpose:	获取部门
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function getDepartment(){
		$Department = M('Department');
		$uid = $this->_uid;
		if($uid){
			//$data = array(
			//	'owner_uid' => $uid
			//);
			//$v = $Category->where($data)->select();
			$v = $Department->select();
			if($v){
				$this->ajaxReturn($v,"获取部门成功",1);
			}else{				
				$this->ajaxReturn(0,"部门为空",0);
			}
		}else{
			$this->ajaxReturn(0,'你确定你登录了吗？',0);
		}

	}
	/*======================================================================*\
		Function:	departmentTranfer
		Purpose:	员工部门调动
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function departmentTranfer(){
		$UserDepartment = M('UserDepartment');
		$uid = $this->_uid;
		$param = $this->_param();
		//$owner_uid = intval($param['uid']);
		if($uid){
			if($uid != 1) $this->ajaxReturn(2,'对不起，您没有权限！',2);
			if($param['department_id'] != "" && $param['user_uid']){
				$data = array(
					'department_id' => $param['department_id'],
					'status' => 1
				);
				$owner_uid = $param['user_uid'];
				$v = $UserDepartment->where("owner_uid = $owner_uid")->save($data);
				if($v){
					$this->ajaxReturn($v,"调动部门成功",1);
				}else{
					$this->ajaxReturn(0,"调动部门失败",0);
				}
			}else{
				$this->ajaxReturn(3,'系统出错，请联系管理员！',3);
			}
		}else{
			$this->ajaxReturn(0,'你确定你登录了吗？',0);
		}

	}
	/*======================================================================*\
		Function:	leaveDepartment
		Purpose:	移除部门
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	
	public function leaveDepartment(){
		$UserDepartment = M('UserDepartment');
		$uid = $this->_uid;
		$owner_uid = intval($param['uid']);
		if($uid && $owner_uid && $param['department_id']){
			$w = array(
				'owner_uid' => $uid,
				'department_id' => $param['department_id'],
			);
			$data['status'] = 0;
			$v = $UserDepartment->where($w)->save($data);
			if($v){
				$this->ajaxReturn($v,"离开部门成功",1);
			}else{				
				$this->ajaxReturn(0,"离开部门失败",0);
			}
		}else{
			$this->ajaxReturn(0,'出错了',0);
		}

	}
	/******************************************************
	*funcion:获取所有部门
	*参数:无
	*目的:获取部门，并display到前台模板
	******************************************************/
	public function departmentList(){
		$Department = M('Department');
		$list = $Department->alias('A')
						   ->join('oa_user_department B ON A.department_id = B.department_id')
						   ->field('A.*,count(B.department_id) department_num')
						   ->where("B.status = 1")
						   ->group('A.department_id')
						   ->select();
		$this->assign("list",$list);
		//dump($list);
		$this->display();
	}
	/******************************************************
	*funcion:部门管理
	*参数:无
	*目的:用于前台调用（load）部门信息
	******************************************************/
	public function departmentManage(){
		//echo $this->_uid;
		if($this->_uid != 1) return;
		$model = M();
		$param = $this->_param();
		$department_id = $param['department_id'];
		$UserDepartment = M('UserDepartment');
		$sql = "SELECT A.*,B.account,C.department_name,D.name FROM oa_user_department A
				LEFT JOIN oa_department C ON C.department_id = A.department_id
				LEFT JOIN oa_user B ON B.uid = A.owner_uid
				LEFT JOIN oa_user_profile D ON D.owner_uid = A.owner_uid
				WHERE A.department_id = $department_id AND A.status = 1";
		$list = $model->query($sql);
		//echo $sql;
		$this->assign("list",$list);
		$this->display();
	}
	/******************************************************
	* funcion:部门日志
	******************************************************/
	public function departmentLogs(){
		$Department = M('Department');
		$User = M('UserDepartment');
		$Worklog = M('Worklog');
		$param = $this->_param();
		$departments = $Department->select();
		$date = date("Y-m-d",time());
		//dump($departments);
		if($param['date']) $date = $param['date'];
		if($param['department'] || $param['department'] == "0"){
			if($param['department'] == "-2"){
				$list = $departments;
			}else{
				foreach($departments as $key=>$value){
					if($value["department_id"] == $param['department']){
						$list[0] = $value;
						break;
					}else{
						continue;
					}
				}
			}
		}else{
			$list = $departments;
		}
		//dump($list);
		foreach($list as $key=>$value){
			$department_id = $value["department_id"];
			//if($param['department']){
			//	if($value["department_id"] == $param['department']){
			//		$department_id = $param['department'];
			//	}else{
			//		continue;
			//	}
			//}
			$list[$key]['worker_list'] = $User->alias('A')
											  ->join('oa_department B ON A.department_id = B.department_id')
											  ->join('oa_user_profile D ON D.owner_uid = A.owner_uid')
											  ->field('A.owner_uid,A.department_id,B.department_name,D.name')
											  ->where("A.department_id = $department_id AND A.status = 1")
											  ->select();
			foreach($list[$key]['worker_list'] as $k=>$value){
				$owner_uid = $value['owner_uid'];
				$list[$key]['worker_list'][$k]['worklog_list'] = $Worklog->where("SUBSTRING_INDEX(create_time,' ',1) = '$date' AND owner_uid = $owner_uid AND status = 1")
																		 ->field('title,work,create_time,worklog_id')->select();
			}
		}
		//$sql = $Worklog->getLastSql();
		//echo $sql;
		$this->assign("department",$departments);
		//dump($list);
		$this->assign("list",$list);
		$this->display();
	}
	/******************************************************
	*funcion:部门日志检索
	******************************************************/
	public function searchDepartmentLog(){
	}
	public function test(){
			$input = "10101010101010101010";
			$input = str_split($input);
			dump($input);
	}
	
}